#include "tiling/platform/platform_ascendc.h"
#include "triu_tiling.h"
#include "register/op_def_registry.h"

namespace optiling
{
    const uint32_t BLOCK_SIZE = 32;
    static ge::graphStatus TilingFunc(gert::TilingContext *context)
    {

        TriuTilingData tiling;
        uint32_t totalLength = context->GetInputTensor(0)->GetShapeSize();
        uint32_t HignAcc = 0;
        uint32_t ub_block_num = 4;
        auto dt = context->GetInputTensor(0)->GetDataType();
        if (totalLength >= 1024*1024){
            HignAcc = 1;
        }
        auto *pvalue = context->GetAttrs()->GetInt(0);
        auto Shape = context->GetInputTensor(0)->GetOriginShape();
        uint32_t xShape = Shape.GetDim(Shape.GetDimNum() - 2);
        uint32_t yShape = Shape.GetDim(Shape.GetDimNum() - 1);
        uint32_t sizeofdatatype;
        uint32_t ALIGN_NUM;
        uint32_t tile_num;
        uint32_t tileLength;
        if (dt == 1)
        { // float16
            sizeofdatatype = 2;
            ub_block_num = 128;
        }
        else if (dt == 0)
        { // float
            sizeofdatatype = 4;
            ub_block_num = 256;
        }
        ALIGN_NUM = BLOCK_SIZE / sizeofdatatype;
        tile_num = totalLength / ALIGN_NUM / ub_block_num;
        tileLength = ALIGN_NUM* ub_block_num;
	printf("tileLength: %d \n",tileLength);
	printf("tileNum: %d \n",tile_num);
        tiling.set_value(*pvalue);
        tiling.set_xShape(xShape);
        tiling.set_yShape(yShape);
        tiling.set_HignAcc(HignAcc);
        tiling.set_tileLength(tileLength);
        tiling.set_tileNum(tile_num);
        context->SetBlockDim(1);
        tiling.set_blockLength(totalLength);
        tiling.SaveToBuffer(context->GetRawTilingData()->GetData(),
                            context->GetRawTilingData()->GetCapacity());
        context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
        size_t *currentWorkspace = context->GetWorkspaceSizes(1);
        currentWorkspace[0] = 0;
        return ge::GRAPH_SUCCESS;
        }
    }
 // namespace optiling

namespace ge
{
    static ge::graphStatus InferShape(gert::InferShapeContext *context)
    {
        const gert::Shape *x1_shape = context->GetInputShape(0);
        gert::Shape *y_shape = context->GetOutputShape(0);
        *y_shape = *x1_shape;
        return GRAPH_SUCCESS;
    }
}

namespace ops
{
    class Triu : public OpDef
    {
    public:
        explicit Triu(const char *name) : OpDef(name)
        {
            this->Input("x")
                .ParamType(REQUIRED)
                .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
                .Format({ge::FORMAT_ND, ge::FORMAT_ND})
                .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
            this->Output("y")
                .ParamType(REQUIRED)
                .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
                .Format({ge::FORMAT_ND, ge::FORMAT_ND})
                .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
            this->Attr("diagonal").Int();

            this->SetInferShape(ge::InferShape);

            this->AICore()
                .SetTiling(optiling::TilingFunc);
            this->AICore().AddConfig("ascend310b");
        }
    };

    OP_ADD(Triu);
}

